import 'package:flutter/material.dart';
import 'package:lz_ui/kd_ui.dart';
import 'package:lz_app_core/extension/kd_extension.dart';

enum KDPwdWarmType {
  // 弱 中 强
  none(''),
  weak('弱'),
  mid('中'),
  strong('强');

  final String value;
  const KDPwdWarmType(this.value);

  static KDPwdWarmType getType(String input) {
    if (input.length < 6) {
      return KDPwdWarmType.none;
    }

    bool hasLetter = KDString.check(input, types: [KDStringType.hasLetter]);
    bool hasNumber = KDString.check(input, types: [KDStringType.hasDigit]);

    /// 对每一个字符判断一下，不是自字母和数字
    bool hasSpecial = false;
    for (var c in input.characters) {
      hasSpecial = !KDString.check(c, types: [KDStringType.letterAndDigit]);
      if (hasSpecial) break;
    }

    int hasTypes = [hasLetter, hasNumber, hasSpecial]
        .map((e) => e ? 1 : 0)
        .toList()
        .reduce((value, element) => value + element);

    // weak
    if (input.length <= 10 && hasTypes == 1) {
      return KDPwdWarmType.weak;
    }

    // mid
    if (input.length >= 6 && input.length <= 10 && hasTypes == 2) {
      return KDPwdWarmType.mid;
    }

    if (input.length >= 10 && hasTypes == 1) {
      return KDPwdWarmType.mid;
    }

    // strong
    if (input.length > 10 && hasTypes == 2) {
      return KDPwdWarmType.strong;
    }

    if (input.length >= 6 && hasTypes == 3) {
      return KDPwdWarmType.strong;
    }

    return KDPwdWarmType.none;
  }

  Color get color {
    switch (this) {
      case weak:
        return KDColor.errRed;
      case mid:
        return KDColor.hex('#FAAD14');
      case strong:
        return KDColor.mainBlue;
      default:
        return KDColor.mainBlue;
    }
  }
}

/// 密码强度提示
class KDPwdWarmWidget extends StatelessWidget {
  final KDPwdWarmType type;

  const KDPwdWarmWidget(this.type, {super.key});

  @override
  Widget build(BuildContext context) {
    if (type == KDPwdWarmType.none) return Container();

    return Row(
      children: [
        Text(
          "强度密度: ",
          style: TextStyle(color: KDColor.place, fontSize: KD.font(24)),
        ),
        Text(type.value,
            style: TextStyle(color: type.color, fontSize: KD.font(24))),
      ],
    );
  }
}
